package com.heraleign.boot.service;

import com.heraleign.boot.bean.Account;
import com.heraleign.boot.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class AccountService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private AccountMapper accountMapper;

    public List<Account> getList(){
        String sql ="SELECT * FROM Account";
        return (List<Account>) jdbcTemplate.query(sql,new RowMapper<Account>(){
            @Override
            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
                Account acc = new Account();
                acc.setAccId(rs.getString("accID"));
                acc.setAccName(rs.getString("accName"));
                acc.setAccAge(rs.getInt("accAge"));
                acc.setAccCode(rs.getString("accCode"));
                acc.setBalance(rs.getDouble("balance"));
                return acc;
            }
        });
    }

    public Account getAcctById(String accId){
        return accountMapper.getAcct(accId);
    }

}

